package uutalk.netcloud.talk.model;


import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.location.BDLocation;

import java.text.SimpleDateFormat;
import java.util.Date;

import uutalk.netcloud.talk.BuildConfig;
import uutalk.netcloud.talk.base.util.thirdpart.UserManager;
import uutalk.netcloud.talk.model.bean.CMDConstant;
import uutalk.netcloud.talk.model.bean.CreatActBean;
import uutalk.netcloud.talk.model.bean.JoinSingleTalkRsp;
import uutalk.netcloud.talk.model.bean.MqttBussinessBean;
import uutalk.netcloud.talk.model.bean.TalkHistoryInfo;

/**
 * Created by hujie on 17/3/31.
 */

public class BussinessBeanFactory {


    /**
     * 根据用户id生成请求服务器用户信息的Json字符串
     * @param userId
     * @return
     */
    public static String createGetUserInfoBean(String userId) {

        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_USER_INFO_CMD);
        bussinessBean.setFrom_type("user");
        if (UserManager.getInstance().getLoginInfo() != null) {
            bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        } else {
            bussinessBean.setFrom_id(userId);
        }
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(userId));
        return jsonObject.toJSONString();


    }

    /**
     * 生成请求服务器的登录信息Json字符串
     * @param userType
     * @param deviceId
     * @param password
     * @return
     */
    public static String createLoginBean(String userType, String deviceId, String password) {

        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.UEER_LOGIN_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(deviceId);
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("account", deviceId);
        jsonObject.put("password", password);
        jsonObject.put("user_type", userType);
        jsonObject.put("client_type", "android");
        jsonObject.put("app_version", BuildConfig.VERSION_NAME);

        return jsonObject.toJSONString();


    }

    /**
     * 生成请求服务器保存用户信息的json
     * @param deviceId
     * @param userId
     * @param name
     * @param picture
     * @param email
     * @param phone
     * @param password
     * @return
     */
    public static String createSetUserInfoBean(String deviceId, String userId, String name, String picture, String email, String phone, String password) {

        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.SET_USER_INFO_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(deviceId);
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(userId));
        jsonObject.put("email", email);
        jsonObject.put("name", name);
        jsonObject.put("picture", picture);
        jsonObject.put("phone", phone);
        if (password != null) {
            jsonObject.put("password", password);
        }

        return jsonObject.toJSONString();


    }

    /**
     * 生成获取当前你所在活动群组的json
     * @return
     */
    public static String createGetActiveGroupBean() {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_ACTIVE_GROUP_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        return jsonObject.toJSONString();
    }


    /**
     *生成获取服务器群组信息的json
     * @param groupID
     * @return
     */
    public static String createGetGroupInfoBean(String groupID) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_GROUP_INFO_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("group_id", Integer.parseInt(groupID));
        return jsonObject.toJSONString();
    }

    /**
     * 创建请求服务器加入群组的json
     * @param userName
     * @param groupID
     * @param password
     * @param authMsg
     * @return
     */
    public static String createJoinGroupBean(
        String userName, String groupID, String password, String authMsg) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.JOIN_GROUP_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("user_name", userName);
        jsonObject.put("group_id", Integer.parseInt(groupID));
        jsonObject.put("password", password);
        jsonObject.put("auth_msg", authMsg);
        return jsonObject.toJSONString();
    }

    /**
     * 创建加入群组的验证请求json
     * @param userName
     * @param groupID
     * @param groupName
     * @param ownerID
     * @param authMsg
     * @return
     */
    public static String createJoinGroupAuthBean(
            String userName, String groupID, String groupName, String ownerID, String authMsg) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.JOIN_GROUP_AUTH_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("user_name", userName);
        jsonObject.put("group_id", Integer.parseInt(groupID));
        jsonObject.put("group_name", groupName);
        jsonObject.put("owner_id", Integer.parseInt(ownerID));
        jsonObject.put("auth_msg", authMsg);
        return jsonObject.toJSONString();
    }

    /**
     * 创建回复加入群组验证的json
     * @param groupID
     * @param groupName
     * @param userID
     * @param ownerID
     * @param authMsg
     * @param errorNo
     * @param errorMsg
     * @return
     */
    public static String createJoinGroupAuthRspBean(String groupID, String groupName, String userID,
                                                    String ownerID,
                                                    String authMsg,
                                                    String errorNo,
                                                    String errorMsg) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.JOIN_GROUP_AUTH_RESP_CMD);
        bussinessBean.setFrom_type("server_cmd");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id(userID);
        bussinessBean.setTo_type("user");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", userID);//Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id())
        jsonObject.put("group_id", Integer.parseInt(groupID));
        jsonObject.put("group_name", groupName);
        jsonObject.put("owner_id", Integer.parseInt(ownerID));
        jsonObject.put("auth_msg", authMsg);
        jsonObject.put("error_no", errorNo);
        jsonObject.put("error_msg", errorMsg);
        return jsonObject.toJSONString();
    }

    /**
     *生成创建一个新的外出活动群组的json
     * @param creatActBean
     * @return
     */
    public static String createActivityBean(CreatActBean creatActBean) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.CREATE_ACTIVE_GROUP_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));

        jsonObject.put("group_name", creatActBean.getGroupName());
        jsonObject.put("group_info", creatActBean.getGroupInfo());
        jsonObject.put("password", creatActBean.getGroupPassword());
        jsonObject.put("pwd_auth", creatActBean.getPwdAuth());
        jsonObject.put("owner_verify", creatActBean.getOwnerVerify());
        return jsonObject.toJSONString();
    }

    /**
     * 生成退出活动群组的json
     * @param cmd
     * @param groupID
     * @return
     */
    public static String createQuitActivityBean(String cmd, String groupID) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(cmd);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("group_id", Integer.parseInt(groupID));
        return jsonObject.toJSONString();
    }

    /**
     * 生成获取群组成员的json

     * @param groupId
     * @return
     */
    public static String createGetGroupMemberBean(String groupId) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_GROUP_MEMBER_CMD);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("group_id", groupId);
        return jsonObject.toJSONString();

    }

    /**
     * 生成上传GPS的json
     * @param groupId
     * @param bdLocation
     * @return
     */
    public static String createUploadGps(String groupId, BDLocation bdLocation) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GPS_INFO);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");


        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("group_id", groupId);
        jsonObject.put("longitude", bdLocation.getLongitude() + "");
        jsonObject.put("latitude", bdLocation.getLatitude() + "");
        jsonObject.put("speed", bdLocation.getSpeed() + "");
        jsonObject.put("direction", bdLocation.getDirection() + "");
        jsonObject.put("altitude", bdLocation.getAltitude() + "");
        jsonObject.put("time", new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(new Date()));

        return jsonObject.toJSONString();

    }

    /**
     * 创建删除群组成员的json
     * @param userName
     * @param userId
     * @param groupId
     * @return
     */
    public static String createDeleteGroupMember(String userName, String userId, String groupId) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.DELETE_GROUP_MEMBER);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", userId);
        jsonObject.put("group_id", groupId);
        jsonObject.put("user_name", userName);
        return jsonObject.toJSONString();

    }

    /**
     * 创建SOS紧急求救的json
     * @param groupID
     * @param msg
     * @param time
     * @return
     */
    public static String createSOSBean(String groupID, String msg, String time) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.USER_SOS);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");

        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));

        jsonObject.put("group_id", groupID);
        jsonObject.put("time", time);
        jsonObject.put("msg", msg);
        return jsonObject.toJSONString();
    }


    /**
     * 创建删除群组成员的json
     * @param groupId
     * @return
     */
    public static String createAcitveGroup(String groupId, String talkType, String receiveId) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.ACTIVE_GROUP);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("group_id", groupId);
        jsonObject.put("talk_type", talkType);
        jsonObject.put("receiver_id", "0".equals(talkType) ? "" : receiveId);
        return jsonObject.toJSONString();

    }


    /**
     * 创建加入单人聊天的json
     * @return
     */
    public static String createJoinSingleTalk(String groupId, String receiverid, String receiverName) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.JOIN_SINGLE_TALK);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("group_id", groupId);
        jsonObject.put("user_name", UserManager.getInstance().getUserInfo().getName());
        jsonObject.put("receiver_id", receiverid);
        jsonObject.put("receiver_name", receiverName);

        return jsonObject.toJSONString();
    }


    /**
     * 创建退出单人聊天的json
     * @return
     */
    public static String createQuitSingleTalk(String groupId, String receiverid, String receiverName) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.QUIT_SINGLE_TALK);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        jsonObject.put("group_id", groupId);
        jsonObject.put("user_name", UserManager.getInstance().getUserInfo().getName());
        jsonObject.put("receiver_id", receiverid);
        jsonObject.put("receiver_name", receiverName);

        return jsonObject.toJSONString();
    }

    /**
     * 创建退出单人聊天的json
     * @return
     */
    public static String createJoinSingleTalkResp(JoinSingleTalkRsp joinSingleTalkRsp) {
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.JOIN_SINGLE_TALK_RESP);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");

        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        if(joinSingleTalkRsp.getUser_id().equals(UserManager.getInstance().getUserInfo().getUser_id())){
            jsonObject.put("user_id", joinSingleTalkRsp.getReceiver_id());
        } else {
            jsonObject.put("user_id", joinSingleTalkRsp.getUser_id());
        }
        jsonObject.put("group_id", joinSingleTalkRsp.getGroup_id());
        jsonObject.put("user_name", joinSingleTalkRsp.getUser_name());
        jsonObject.put("receiver_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("receiver_name", joinSingleTalkRsp.getReceiver_name());
        jsonObject.put("error_no",joinSingleTalkRsp.getError_no());
        return jsonObject.toJSONString();
    }


    /**
     * 创建获取用户加入过的群组的json
     * @return
     */
    public static String createJoinedGroups(){
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_USER_JOINED_GROUPS);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", Integer.parseInt(UserManager.getInstance().getUserInfo().getUser_id()));
        return jsonObject.toJSONString();
    }

    /**
     * 创建添加好友的json
     * @param friendID
     * @return
     */
    public static String createAddSigleFriend(String friendID){
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.ADD_SIGLE_FRIEND);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("friend_id", friendID);
        return jsonObject.toJSONString();
    }

    /**
     * 删除好友的json
     * @param friendID
     * @return
     */
    public static String createDeleteSigleFriend(String friendID){
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.DELELE_SIGLE_FRIEND);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("friend_id", friendID);
        return jsonObject.toJSONString();
    }


    /**
     * 获取好友列表
     * @param userID
     * @return
     */
    public static String createGetSigleFriendList(String userID){
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.GET_SIGLE_FRIEND_LIST);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", userID);
        return jsonObject.toJSONString();
    }

    public static String createUploadTalkHistory(TalkHistoryInfo talkHistoryInfo){
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.UPLOAD_TALK_HISTORY);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("time", talkHistoryInfo.getTime());
        jsonObject.put("group_id", talkHistoryInfo.getGroupID());
        jsonObject.put("talk_type", talkHistoryInfo.getTalkType());
        jsonObject.put("user_name", talkHistoryInfo.getUserName());
        jsonObject.put("voice_length", talkHistoryInfo.getVoice_length()+"");
        jsonObject.put("voice_url", talkHistoryInfo.getVoice_url());
        return jsonObject.toJSONString();
    }

    public static String createDownloadTalkHistory(String groupID, String updateTime){
        if(TextUtils.isEmpty(groupID)){
            return null;
        }
        MqttBussinessBean bussinessBean = new MqttBussinessBean();
        bussinessBean.setCmd(CMDConstant.DOWNLOAD_TALK_HISTORY);
        bussinessBean.setFrom_type("user");
        bussinessBean.setFrom_id(UserManager.getInstance().getLoginInfo().getDeviceId());
        bussinessBean.setTo_id("");
        bussinessBean.setTo_type("server_cmd");
        JSONObject jsonObject = (JSONObject) JSON.toJSON(bussinessBean);
        jsonObject.put("user_id", UserManager.getInstance().getUserInfo().getUser_id());
        jsonObject.put("group_id", groupID);
        if(groupID.contains("_")){
            jsonObject.put("talk_type", "1");
        } else {
            jsonObject.put("talk_type", "0");
        }

        jsonObject.put("update_time", updateTime);
        return jsonObject.toJSONString();
    }


}
